Skip to content

Set SERVER_PORT appropriately #1628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

ac000
Copy link
Member

@ac000 ac000 commented Jul 4, 2025

The Perl, PHP, Python and Ruby language modules all hard code
SERVER_PORT to "80".

Adjust them to bring them in line with the wasm language module which
uses r->local_port (I.e. the port unit accepted the connection on).

Closes: https://github.com/nginx/unit/issues/761
Signed-off-by: Andrew Clayton <[email protected]>

@ac000 ac000 marked this pull request as ready for review July 4, 2025 15:57
@ac000 ac000 requested review from hongzhidao July 4, 2025 15:59
@ac000
Copy link
Member Author

ac000 commented Jul 23, 2025

@hongzhidao ping...

Hi Zhidao, I'm planning on doing a 1.35.0 release soon and would like to
have this patch included...

@ac000
Copy link
Member Author

ac000 commented Jul 23, 2025

  • Python ASGI also needed updating...
$ git range-diff d0dafc2e...44752135
1:  d0dafc2e ! 1:  44752135 Set SERVER_PORT appropriately
    @@ src/perl/nxt_perl_psgi.c: nxt_perl_psgi_env_create(PerlInterpreter *my_perl,
          for (i = 0; i < r->fields_count; i++) {
              f = r->fields + i;
     
    + ## src/python/nxt_python_asgi.c ##
    +@@ src/python/nxt_python_asgi.c: nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req,
    +     PyObject            *scope, *v, *type, *scheme;
    +     PyObject            *headers, *header;
    +     nxt_str_t           prefix;
    ++    unsigned long       port;
    +     nxt_unit_field_t    *f;
    +     nxt_unit_request_t  *r;
    + 
    +@@ src/python/nxt_python_asgi.c: nxt_py_asgi_create_http_scope(nxt_unit_request_info_t *req,
    +     SET_ITEM(scope, client, v)
    +     Py_DECREF(v);
    + 
    +-    v = nxt_py_asgi_create_address(&r->local_addr, r->local_addr_length, 80);
    ++    p = nxt_unit_sptr_get(&r->local_port);
    ++    port = strtoul(p, NULL, 10);
    ++
    ++    v = nxt_py_asgi_create_address(&r->local_addr, r->local_addr_length, port);
    +     if (nxt_slow_path(v == NULL)) {
    +         nxt_unit_req_alert(req, "Python failed to create 'server' pair");
    +         goto fail;
    +
      ## src/python/nxt_python_wsgi.c ##
     @@ src/python/nxt_python_wsgi.c: static PyTypeObject nxt_py_input_type = {
      

@hongzhidao
Copy link
Contributor

@hongzhidao ping...

Hi Zhidao, I'm planning on doing a 1.35.0 release soon and would like to have this patch included...

Hi @ac000,
Sorry that it seems I missed the GH notifications with the ticket.
LGTM, thanks.

The Perl, PHP, Python, Ruby & Java language modules all hard code
SERVER_PORT to "80".

Adjust them to bring them in line with the wasm language module which
uses r->local_port (I.e. the port unit accepted the connection on).

Closes: nginx#761
Signed-off-by: Andrew Clayton <[email protected]>
@ac000
Copy link
Member Author

ac000 commented Jul 25, 2025

  • May as well do Java while we're at it...
$ git range-diff 44752135...c8b859e3
1:  44752135 ! 1:  c8b859e3 Set SERVER_PORT appropriately
    @@ Metadata
      ## Commit message ##
         Set SERVER_PORT appropriately
     
    -    The Perl, PHP, Python and Ruby language modules all hard code
    +    The Perl, PHP, Python, Ruby & Java language modules all hard code
         SERVER_PORT to "80".
     
         Adjust them to bring them in line with the wasm language module which
    @@ Commit message
         Closes: https://github.com/nginx/unit/issues/761
         Signed-off-by: Andrew Clayton <[email protected]>
     
    + ## src/java/nxt_jni_Request.c ##
    +@@ src/java/nxt_jni_Request.c: nxt_java_Request_getServerName(JNIEnv *env, jclass cls, jlong req_ptr)
    + static jint JNICALL
    + nxt_java_Request_getServerPort(JNIEnv *env, jclass cls, jlong req_ptr)
    + {
    +-    jint                res;
    +-    char                *host, *colon, tmp;
    +-    nxt_unit_field_t    *f;
    ++    char                *p;
    +     nxt_unit_request_t  *r;
    + 
    +     r = nxt_jlong2ptr(req_ptr);
    + 
    +-    f = nxt_java_findHeader(r->fields, r->fields + r->fields_count,
    +-                            "Host", 4);
    +-    if (f != NULL) {
    +-        host = nxt_unit_sptr_get(&f->value);
    ++    p = nxt_unit_sptr_get(&r->local_port);
    + 
    +-        colon = memchr(host, ':', f->value_length);
    +-
    +-        if (colon == NULL) {
    +-            return 80;
    +-        }
    +-
    +-        tmp = host[f->value_length];
    +-
    +-        host[f->value_length] = '\0';
    +-
    +-        res = strtol(colon + 1, NULL, 10);
    +-
    +-        host[f->value_length] = tmp;
    +-
    +-        return res;
    +-    }
    +-
    +-    return nxt_java_Request_getLocalPort(env, cls, req_ptr);
    ++    return strtol(p, NULL, 10);
    + }
    + 
    + 
    +
      ## src/nxt_php_sapi.c ##
     @@ src/nxt_php_sapi.c: nxt_php_register_variables(zval *track_vars_array TSRMLS_DC)
      

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants